
#include <stdlib.h>
#include "PrintString.h"
#include "include.h"
#include "GL/glut.h"

PrintString::PrintString(TextureManager * textureManager, char * string, Vector2D position, float size)
{
	this->textureManager = textureManager;
	this->string = string;
	this->position = position;
	this->size = size;
	if(textureManager)
	{
		if(!textureManager->LoadTexture(FONT_TEXTURE))
			printf("Failed to load %s!\n", FONT_TEXTURE);
	}
}
PrintString::~PrintString()
{
	if(string != NULL)
		delete(string);
	string = NULL;
}
int PrintString::GetLength()
{
	return strlen(string);
}
void PrintString::SetPosition(Vector2D position)
{
	this->position = position;
}
void PrintString::SetSize(float size)
{
	this->size = size;
}
char * PrintString::GetString()
{
	return string;
}
char * PrintString::Click(Vector2D mousePosition)
{
	printf("CLICK AT (%f, %f)\n", mousePosition.x, mousePosition.y);
	Vector2D bottomRight = Vector2D(position.x + size * strlen(string), position.y - size);
	
	bottomRight = bottomRight * 100;
	if(bottomRight.x - (int)bottomRight.x >= 0.5)
		bottomRight.x = (int)bottomRight.x + 1;
	if(bottomRight.y - (int)bottomRight.y >= 0.5)
		bottomRight.y = (int)bottomRight.y + 1;
		
	Vector2D roundedPosition = position * 100;
	if(roundedPosition.x - (int)roundedPosition.x >= 0.5)
		roundedPosition.x = (int)roundedPosition.x + 1;
	if(roundedPosition.y - (int)roundedPosition.y >= 0.5)
		roundedPosition.y = (int)roundedPosition.y + 1;
	
	if(mousePosition.x >= roundedPosition.x && mousePosition.x <= bottomRight.x &&
	   mousePosition.y >= bottomRight.y && mousePosition.y <= roundedPosition.y)
	{
		return string;
	}
	return NULL;	
}
void PrintString::Draw(Vector2D parentPosition)
{
	Vector2D printPosition = position + parentPosition;
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,100,100,0, -1, 1);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(0, 0, -0.5);
	if(textureManager)
		textureManager->EnableTexture(FONT_TEXTURE);
		
  	glEnable( GL_BLEND);
  	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
  	glBlendFunc(GL_ONE, GL_ONE);
	glAlphaFunc( GL_GREATER, 0.05 );
  	glEnable( GL_ALPHA_TEST );
  	glColor3f(1,1,1);

	glEnable(GL_TEXTURE_2D);
	for(int i=0; i < strlen(string); i++)
	{	
		
		if(string[i] > '!' && string[i] < '~')
		{
			glBegin(GL_QUADS);
			/*printf("drawing letter (%c) at\n\t(%f, %f)\n\t(%f, %f)\n\t(%f,%f)\n\t(%f, %f)\n",
				string[i],
				printPosition.x + i * size, printPosition.y,
				printPosition.x + (i + 1) * size, printPosition.y,
				printPosition.x + (i + 1) * size, printPosition.y + size,
				printPosition.x + i * size, printPosition.y + size);*/
			Vector2D * textureVertices = GetVertices(string[i]);
			glTexCoord2f(textureVertices[0].x, textureVertices[0].y);
			glVertex3f(printPosition.x + i * size, printPosition.y, 1);
			
			glTexCoord2f(textureVertices[1].x, textureVertices[1].y);
			glVertex3f(printPosition.x + (i + 1) * size, printPosition.y, 1);
			
			glTexCoord2f(textureVertices[2].x, textureVertices[2].y);
			glVertex3f(printPosition.x + (i + 1) * size, printPosition.y + size, 1);
			
			glTexCoord2f(textureVertices[3].x, textureVertices[3].y);
			glVertex3f(printPosition.x + i * size, printPosition.y + size, 1);
			glEnd();
		}	
	}
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}
void PrintString::Draw()
{
	Draw(Vector2D(0,0));
}
Vector2D * PrintString::GetVertices(char c)
{
	int index = (int)c - (int)'!';
	int column = index % 10;
	int row = index/10;
	int columnSpacing[11] = {0,14,44,71,94,123,152,178,201,227, 250};
	int rowSpacing[11] = {0,22,46,70,94,119,143,167,192,217,238};
	Vector2D * vertices = (Vector2D *)malloc(sizeof(Vector2D) * 4);
	vertices[0] = Vector2D(	columnSpacing[column]/256.0, 1-rowSpacing[row]/256.0);
	vertices[1] = Vector2D(columnSpacing[column + 1]/256.0, 1-rowSpacing[row]/256.0);
	vertices[2] = Vector2D(columnSpacing[column + 1]/256.0, 1-rowSpacing[row+1]/256.0);
	vertices[3] = Vector2D(	columnSpacing[column]/256.0, 1-rowSpacing[row+1]/256.0);
	
	return vertices;
}
